﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="PlaneExtensions.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;

    #endregion

    public enum PlaneSide
    {
        NO_SIDE,
        POSITIVE_SIDE,
        NEGATIVE_SIDE,
        BOTH_SIDE,
    }

    public static class PlaneExtensions
    {
        public static float getDistance(this Plane p, Vector3 point)
        {
            return Vector3.Dot(p.Normal, point) + p.D;
        }

        public static PlaneSide getSide(this Plane p, Vector3 point)
        {
            float distance = p.getDistance(point);

            if (distance < 0.0f)
            {
                return PlaneSide.NEGATIVE_SIDE;
            }

            if (distance > 0.0f)
            {
                return PlaneSide.POSITIVE_SIDE;
            }

            return PlaneSide.NO_SIDE;
        }

        public static PlaneSide getSide(this Plane p, BoundingBox box)
        {
            if (box.IsNull())
            {
                return PlaneSide.NO_SIDE;
            }

            if (box.IsInfinite())
            {
                return PlaneSide.BOTH_SIDE;
            }

            return p.getSide(box.getCenter(), box.getHalfSize());
        }

        public static PlaneSide getSide(this Plane p, Vector3 center, Vector3 halfSize)
        {
            float dist = p.getDistance(center);
            float maxAbsDist = Math.Abs(Vector3.Dot(p.Normal, halfSize));

            if (dist < -maxAbsDist)
            {
                return PlaneSide.NEGATIVE_SIDE;
            }

            if (dist > +maxAbsDist)
            {
                return PlaneSide.POSITIVE_SIDE;
            }

            return PlaneSide.BOTH_SIDE;
        }
    }
}
